<?php

/**
* @category   Oos
* @package    Oos_Site
* @license    New BSD License
*/


/**
* @see Zend_Gdata
*/
require_once 'Zend/Gdata.php';
require_once 'Oos/Site/SiteFeed.php';
require_once 'Oos/Site/SiteEntry.php';

/**
*
* @category   Oos
* @package    Oos
* @license    http://framework.zend.com/license/new-bsd     New BSD License
*/
class Oos_Site extends Zend_Gdata
{

       protected $_defaultPostUri;
       protected $_user_slug;
       protected $_entrypoint;
       protected $_apiKey;
       protected $_apiSign;
       
   public static $namespaces = array(
                                     'oos' => 'http://11870.com/api/oos',
                                     'georss' => 'http://www.georss.org/georss/10',
                                     'gml' => 'http://www.opengis.net/gml');

   /**
    * Create Oos_Site object
    */
   public function __construct($client = null, $entrypoint, $user_slug, $apiKey = null, $apiSecret = null)
   {
       $this->registerPackage('Oos_Site');
       $this->registerPackage('Oos_Site_Extension');
       
       $this->_entrypoint = $entrypoint;
       $this->_user_slug = $user_slug;
       $this->_apiKey = $apiKey;
       
       if ($apiSecret != null) {
               $this->_apiSign = md5($apiKey . $apiSecret);
       }
       
        
	   //$this->_defaultPostUri = $defaultposturi;
       parent::__construct($client);
   }

   protected function decorateEntries($feed, $curslug = null) {
       foreach ($feed->entries as $entry) {
                       $this->decorateEntry($entry, $curslug);        
       }
   }
   
   protected function decorateEntry($entry, $curslug = null) {
       if ($curslug == null)
               $curslug = $this->_user_slug;

       $entry->setTagScheme($this->getTagScheme($curslug));
       $entry->setListScheme($this->getListScheme($curslug));
   }
   
   /**
    * Retreive feed object
    *
    * @param mixed $location The location for the feed, as a URL, user_slug or Query
    * @return Oos_Site_SiteFeed
    */
   public function getSiteFeed($location = null)
   {
       $uri = null;
       $curslug = null;
       if ($location == null) {
           $uri = $this->getSitesFeedURI();
       } else if ($location instanceof Zend_Gdata_Query) {
           if ($this->_apiKey != null) {
               $location->setParam("appToken", $this->_apiKey);
               $location->setParam("authSign", $this->_apiSign);
           }
           $uri = $location->getQueryUrl();
           //echo $uri;
       } else {
               if (Zend_Uri::check($location)) {
                       $location = new Zend_Gdata_Query($location);
                       if ($this->_apiKey != null) {
                       $location->setParam("appToken", $this->_apiKey);
                       $location->setParam("authSign", $this->_apiSign);
                       }
               $uri = $location->getQueryUrl();
               } else {
                       $q = new Zend_Gdata_Query($this->getSitesFeedURI($location));
                       $curslug = $location;
                       if ($this->_apiKey != null) {
                       $q->setParam("appToken", $this->_apiKey);
                       $q->setParam("authSign", $this->_apiSign);
                       }
                       $uri = $q->getQueryUrl();
               }
       }
       
       $feed = parent::getFeed($uri, 'Oos_Site_SiteFeed');
       
       $this->decorateEntries($feed, $curslug);
       
       return $feed;
   }

   
   
   /**
    * Retreive entry object
    *
    * @return Oos_Site_SiteEntry
    */
   public function getSiteEntry($location = null)
   {
       $curslug = null;
       if ($location == null) {
           require_once 'Zend/Gdata/App/InvalidArgumentException.php';
           throw new Zend_Gdata_App_InvalidArgumentException(
                   'Location must not be null');
       } else if ($location instanceof Zend_Gdata_Query) {
               if ($this->_apiKey != null) {
               $location->setParam("appToken", $this->_apiKey);
               $location->setParam("authSign", $this->_apiSign);
               }
           $uri = $location->getQueryUrl();
       } else {
           if (Zend_Uri::check($location)) {
                       $location = new Zend_Gdata_Query($location);
                       if ($this->_apiKey != null) {
                       $location->setParam("appToken", $this->_apiKey);
                       $location->setParam("authSign", $this->_apiSign);
                       }
               $uri = $location->getQueryUrl();
               } else {
                       $curslug = $location;
                       $q = new Zend_Gdata_Query($this->getSitesURI($location));
                       if ($this->_apiKey != null) {
                       $q->setParam("appToken", $this->_apiKey);
                       $q->setParam("authSign", $this->_apiSign);
                       }
                       $uri = $q->getQueryUrl();
               }
       }
       $entry = parent::getEntry($uri, 'Oos_Site_SiteEntry');
       $this->decorateEntry($entry, $curslug);
       return $entry;
   }

   public function insertSite($site, $uri=null)
   {
       if ($uri == null) {
           $uri = $this->getSitesFeedURI();
       }

       $newSite = $this->insertEntry($site, $uri, 'Oos_Site_SiteEntry');
       return $newSite;
   }

   
 public function getTagScheme($user = null) {
       if ($user == null)
               $u = $this->_user_slug;
       else
               $u = $user;
       
   return $this->_entrypoint . '/tags/' . $u;
 }

 public function getListScheme($user = null) {
       if ($user == null)
               $u = $this->_user_slug;
       else
               $u = $user;
       
   return $this->_entrypoint . '/lists/' . $u;
 }

 public function getSitesFeedURI($user = null) {
       if ($user == null)
         $user = $this->_user_slug;
       return $this->_entrypoint . '/sites/' . $user;
 }

 public function setUserSlug($value) {
   $this->_user_slug = $value;
 }
 
 
 public function newSiteEntry($user = null) {
       if ($user = null)
         $user = $this->_user_slug;


       $e = new Oos_Site_SiteEntry();
       $this->decorateEntry($e, $user);
       $e->setAuthorName($user);
       return $e;
 }

}

?>